package pl.abstractvoid.gui;

import java.io.IOException;
import java.io.OutputStream;
import javax.swing.JTextArea;

/**
 * Class responsible from redirecting System.out to a swing component, namely
 * JTextArea in RConsolePanel, so that it emulates the console.
 * @author Ivo Zieliński
 */
public class ConsoleAreaOutputStream extends OutputStream {

    private final JTextArea textArea;

    /**
     * Strim is bound to a JTextArea component
     *
     * @param textArea
     */
    public ConsoleAreaOutputStream(JTextArea textArea) {
        this.textArea = textArea;
    }

    @Override
    public void flush() {
    }

    @Override
    public void close() {
    }

    /**
     * Redirect all text to the textArea.
     *
     * @param b
     * @throws IOException
     */
    @Override
    public void write(int b) throws IOException {
        textArea.append(String.valueOf((char) b));
    }
}
